<template>
  <!-- 导航栏 -->
  <div class="authorize-admin">
    <van-nav-bar class="page-nav-bar" left-text="管理员身份认证" left-arrow @click-left="$router.back()" />
    <!-- / 导航栏 -->

    <!-- 头像部分 -->
    <div class="header">
      <div class="login-btn" @click="!user && $router.push('/login')">
        <van-image v-if="user" round class="avatar" :src="user.avatar || require('@/assets/avatar.jpg')" fit="cover" />
        <div v-else class="login-item">
          <van-icon name="user-circle-o" />
        </div>
        <span v-if="user" class="text">{{ user.nickname || user.username }}</span>
        <span v-else class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- <div class="h-photo">
      <div class="box">
        <span class="tx"><van-icon name="user-circle-o" /></span>
        <span class="name">{{ $store.state.user.nickname || $store.state.user.username || '未登录' }}</span>
      </div>
    </div> -->
    <!-- / 头像部分 -->

    <!-- 内容模块 -->
    <div class="footer">
      <div class="content">
        <van-form @submit="submit" ref="authorizeForm">
          <div class="me"><span>请输入您的用户名和密码进行验证</span></div>
          <div class="form-x">
            <van-field
              class="field"
              v-model="userInfo.username"
              name="username"
              label="用户账户:"
              :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
              class="field"
              v-model="userInfo.password"
              type="password"
              name="password"
              label="用户密码:"
              :rules="[{ required: true, message: '请填写密码' }]"
            />
          </div>
        </van-form>
      </div>
    </div>
    <!-- / 内容模块 -->

    <!-- 底部按钮 -->
    <div class="bottom" @click="submit">
      <input type="button" value="提交认证" />
    </div>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user' // 获取用户信息
import { mapState } from 'vuex'

export default {
  name: 'AuthorizeAdmin',
  data () {
    return {
      userInfo: {
        username: '',
        password: ''
      }
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created () {},
  mounted () {},
  methods: {
    async submit () {
      try {
        await this.$refs.authorizeForm.validate()
        this.$toast.loading({
          message: '登录中...', // 展示时的提示信息
          forbidClick: true, // 展示时, 静止点击背景
          duration: 0 // toast 展示时间 默认为2秒, 0 表示持续展示
        })
        await getUserInfo(this.userInfo)
        this.$toast.success('认证成功')
        this.$refs.authorizeForm.resetValidation()
        this.$router.back()
      } catch (error) {
        console.log('认证失败: ', error)
        this.$toast.fail('认证失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.authorize-admin {
  /deep/ .page-nav-bar {
    .van-icon-arrow-left,
    .van-nav-bar__text {
      font-size: 35px;
      font-weight: 700;
    }
  }

  .header {
    height: 361px;
    background-color: #3f51b5 !important;
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      padding-bottom: 72px;
      .login-item {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
        background-color: #fff;
        border-radius: 50%;
        i.van-icon {
          font-size: 80px;
          color: #3296fa;
        }
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
      .avatar {
        width: 136px;
        height: 136px;
        margin-bottom: 20px;
      }
    }
  }

  // .h-photo {
  //   display: flex;
  //   height: 350px;
  //   width: 100%;
  //   background-color: #3f51b5;
  //   justify-content: center;
  //   align-items: flex-start;
  //   .box {
  //     display: flex;
  //     width: 200px;
  //     height: 270px;
  //     color: #fff;
  //     justify-content: center;
  //     flex-direction: column;
  //     text-align: center;
  //     .tx {
  //       font-size: 100px;
  //     }
  //     .name {
  //       font-size: 35px;
  //     }
  //   }
  // }

  .footer {
    position: relative;
    font-size: 30px;
    .content {
      position: absolute;
      top: -20px;
      left: 50%;
      transform: translate(-50%, -5%);
      width: 90%;
      height: 800px;
      border-radius: 1%;
      padding: 30px 0;
      background-color: #fff;
      .me {
        padding-left: 20px;
        border-bottom: 1px solid #ddd;
        span {
          padding-left: 25px;
          line-height: 80px;
          border-left: 10px solid #3f51b5;
        }
      }
      .form-x {
        padding: 30px 30px;
        .field {
          border-bottom: 1px solid #ddd;
          line-height: 60px;
          margin-top: 30px;
        }
        .van-cell::after {
          border-bottom: 0;
        }
      }
    }
  }

  .bottom {
    padding-top: 785px;
    input {
      border: 0;
      width: 100%;
      color: #fff;
      height: 100px;
      font-size: 33px;
      background-color: #3f51b5;
    }
  }
}
</style>
